package team.pikachu.pikachuclient.message;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.google.gson.Gson;


import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import team.pikachu.pikachuclient.R;
import team.pikachu.pikachuclient.beans.temp.MessageTemp;
import team.pikachu.pikachuclient.utils.HttpUtils;
import team.pikachu.pikachuclient.utils.IPConstants;
import team.pikachu.pikachuclient.utils.LogUtil;
import team.pikachu.pikachuclient.utils.Constants;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link MsgUploadFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link MsgUploadFragment#newInstance} factory method to
 * create an instance of this fragment.
 *
 * 消息上传页面
 */

@Deprecated
public class MsgUploadFragment extends Fragment {
    private static final String LOGTAG = LogUtil.makeLogTag(MsgUploadFragment.class);

    private OnFragmentInteractionListener mListener;

    private SharedPreferences pre;

    @BindView(R.id.event_name)
    EditText eventNameText;
    @BindView(R.id.location_id)
    EditText locationIdText;
    @BindView(R.id.room)
    EditText roomText;
    @BindView(R.id.detail)
    EditText detailText;

    public MsgUploadFragment() {
        // Required empty public constructor
    }

    public static MsgUploadFragment newInstance() {
        MsgUploadFragment fragment = new MsgUploadFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_msg_upload, container, false);
        ButterKnife.bind(this,view);
        return view;
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
//        if (context instanceof OnFragmentInteractionListener) {
//            mListener = (OnFragmentInteractionListener) context;
//        } else {
//            throw new RuntimeException(context.toString()
//                    + " must implement OnFragmentInteractionListener");
//        }
        if(context instanceof Activity){
            pre = context.getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }

    @OnClick(R.id.submit)
    void onSubmit(){
        String eventName = eventNameText.getText().toString();
        String locationId = locationIdText.getText().toString();
        String room = roomText.getText().toString();
        String detail = detailText.getText().toString();
        int userId = pre.getInt("USERNAME",-1);
        int roleId = pre.getInt("ROLE",1);


        View focusView = null;
        Boolean cancel = false;

        if(TextUtils.isEmpty(eventName)){
            eventNameText.setError("请输入故障简介");
            focusView = eventNameText;
            cancel = true;
        }else if(TextUtils.isEmpty(locationId)){
            locationIdText.setError("请输入故障地点");
            focusView = locationIdText;
            cancel = true;
        }

        if(cancel){
            focusView.requestFocus();
            return ;
        }else {
            MessageTemp msg = new MessageTemp(eventName, 1 ,room, detail, Constants.STATUS_WAITING, userId, roleId, Constants.OP_UPLOAD);
            Gson gson = new Gson();
            String json = gson.toJson(msg);
            Log.e(LOGTAG,"1111");
            UploadAsy uploadAsy = new UploadAsy();
            uploadAsy.execute(new String[]{IPConstants.ACCEPT, json});
        }
    }

    public class UploadAsy extends AsyncTask<String,Void,String>{

        @Override
        protected String doInBackground(String... params) {
            return HttpUtils.sendJson(params[0],params[1]);
        }

        @Override
        protected void onPostExecute(String aBoolean) {
            super.onPostExecute(aBoolean);
            if(aBoolean.equals("success")){
                Log.d(LOGTAG,"success");
            } else {
                Log.d(LOGTAG,"fail");
            }
        }
    }

}
